package com.jf3q.front.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jf3q.common.domain.EduChapter;
import com.jf3q.common.domain.EduCourse;
import com.jf3q.common.domain.EduSubject;
import com.jf3q.common.domain.EduVideo;
import com.jf3q.common.service.ChapterService;
import com.jf3q.common.service.EduCourseService;
import com.jf3q.common.service.VideoService;
import com.jf3q.common.service.impl.EduCourseServiceImpl;
import com.jf3q.common.service.impl.EduSubjectServiceImpl;
import com.jf3q.common.vo.CourseVo;
import com.jf3q.common.vo.ResultVo;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    EduCourseServiceImpl eduCourseService;

    @Autowired
    EduCourseService courseService;
    @Autowired
    ChapterService chapterService;
    @Autowired
    VideoService videoService;
    @Autowired
    EduSubjectServiceImpl subjectService;
    // 页面展示
    @PostMapping("/getCourse/{page}/{limit}")
    public ResultVo getCourse(@PathVariable(required = true)Integer page, @RequestParam(defaultValue = "8")Integer limit){
        Page<EduCourse> list = eduCourseService.page(new Page<>(page, limit));
        return ResultVo.success(list);
    }

    // 获取一二级分类
    @GetMapping("/getSubject")
    public ResultVo getSubject(){
        List<EduSubject> tree=new ArrayList<>();
        List<EduSubject> list = subjectService.list();
        for (EduSubject eduSubject : list) {
            if(eduSubject.getParentId().equals("0")){
            tree.add(findChiler(eduSubject,list));
            }
        }
        return ResultVo.success(tree);
    }

    private EduSubject findChiler(EduSubject eduSubject, List<EduSubject> list) {
        eduSubject.setChildren(new ArrayList<>());
        for (EduSubject subject : list) {
            if(subject.getParentId().equals(eduSubject.getId())){
                eduSubject.getChildren().add(subject);
                findChiler(subject,list);
            }
        }
        return eduSubject;
    }
    //课程详情查询
    @GetMapping("/{courseId}")
    public ResultVo getCourse(@PathVariable String courseId){
        CourseVo courseVo = courseService.getCourseId(courseId);
        //查询章节小节
        LambdaQueryWrapper<EduChapter> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EduChapter::getCourseId,courseId);
        queryWrapper.orderByAsc(EduChapter::getSort);
        List<EduChapter> chapters = chapterService.list(queryWrapper);
        for (EduChapter chapter : chapters) {
            LambdaQueryWrapper<EduVideo> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(EduVideo::getChapterId,chapter.getId());
            wrapper.orderByAsc(EduVideo::getSort);
            chapter.setVideos(videoService.list(wrapper));
        }
        courseVo.setChapters(chapters);
        return ResultVo.success("",courseVo);
     }
}
